package com.xnew.user.controller;

import com.alibaba.fastjson.JSONObject;
import com.xnew.aes.LogParameter;
import com.xnew.bean.common.IndvUserInfo;
import com.xnew.bean.common.SysUserInfo;
import com.xnew.entity.ssoApi.*;
import com.xnew.exception.CustomException;
import com.xnew.ssoApi.mapper.SysUserInfoMapper;
import com.xnew.ssoApi.service.SsoApiService;
import com.xnew.user.service.IndvUserInfoService;
import com.xnew.user.service.UserInfoService;
import com.xnew.utils.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: hexs
 * @Project: JavaLaity
 * @Pcakage: com.xnew.user.controller.IndvUserInfoController
 * @Date: 2022年05月19日 16:57
 * @Description: 个人用户管理
 */
@RestController
@RequestMapping(value = "/indvUser")
public class IndvUserInfoController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private IndvUserInfoService indvUserInfoService;

    @Resource
    private UserInfoService userInfoService;

    /**
     * 个人用户模块查询列表
     * @param input
     * @return
     */
    @CrossOrigin
    @LogParameter(key = "", desc = "个人用户模块查询列表")
    @RequestMapping(value = "/queryIndvUserList", method = RequestMethod.POST)
    public DataOutput queryIndvUserList(@RequestBody DataInput input, HttpServletRequest request) throws Exception{
        DataOutput out = DataOutput.success();
        try {
            SysUserInfo userInfo = UserUtil.getUserInfo(request);
            List<SysUserInfo> list = userInfoService.queryIndvUserList(input, userInfo);
            out = MybatisParam.getOutput(ConvertEntityUtil.convertListData(list));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            out.setCode(-90001);
            out.setMsg("操作失败！");
        }
        return out;
    }

    /**
     * 新增个人用户
     * @param input
     * @return
     */
    @CrossOrigin
    @LogParameter(key = "", desc = "新增个人用户")
    @RequestMapping(value = "/indvUserAdd", method = RequestMethod.POST)
    public DataOutput indvUserAdd(@RequestBody DataInput input, HttpServletRequest request) throws Exception{
        DataOutput out = DataOutput.success();
        IndvUserInfo indvUserInfo = new IndvUserInfo();
        if (
                input.getParam() == null ||
                input.getParam().get("indvUserName") == null ||
                input.getParam().get("loginName") == null ||
                input.getParam().get("beltoDeptNo") == null ||
                input.getParam().get("mob") == null ||
                input.getParam().get("eml") == null
        ){
            out.setCode(-90001);
            out.setMsg("必填字段不存在！");
            return out;
        }if(
                StringUtils.isEmpty(input.getParam().get("indvUserName").toString()) ||
                StringUtils.isEmpty(input.getParam().get("loginName").toString()) ||
//                StringUtils.isEmpty(input.getParam().get("beltoDeptNo").toString()) ||
                StringUtils.isEmpty(input.getParam().get("mob").toString()) ||
                StringUtils.isEmpty(input.getParam().get("eml").toString())
        ){
            out.setCode(-90001);
            out.setMsg("字段不能为空！");
            return out;
        }
        try {
            indvUserInfo =(IndvUserInfo) ConvertEntityUtil.convertEntityByParam(input.getParam(), indvUserInfo);
            out = indvUserInfoService.indvUserAdd(indvUserInfo, request);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            out.setCode(-90001);
            out.setMsg("操作失败！" + e.getMessage());
        }
        return out;
    }

    /**
     * 查询个人用户
     * @param input
     * @return
     */
    @CrossOrigin
    @LogParameter(key = "", desc = "查询个人用户")
    @RequestMapping(value = "/queryIndvUser", method = RequestMethod.POST)
    public DataOutput queryIndvUser(@RequestBody DataInput input, HttpServletRequest request) throws Exception{
        DataOutput out = DataOutput.success();
        if (
            input.getParam() == null ||
            input.getParam().get("indvUserNo") == null ||
            StringUtils.isEmpty(input.getParam().get("indvUserNo").toString())
        ){
            out.setCode(-90001);
            out.setMsg("缺少必填字段！");
            return out;
        }
        try {
            IndvUserInfo indvUserInfo = indvUserInfoService.queryIndvUser(input.getParam().get("indvUserNo").toString(), request);
            out.setData(ConvertEntityUtil.convertEntityData(indvUserInfo));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            out.setCode(-90001);
            out.setMsg("操作失败！");
        }
        return out;
    }

    /**
     * 修改个人用户
     * @param input
     * @return
     */
    @CrossOrigin
    @LogParameter(key = "", desc = "修改个人用户")
    @RequestMapping(value = "/indvUserUpdate", method = RequestMethod.POST)
    public DataOutput indvUserUpdate(@RequestBody DataInput input, HttpServletRequest request) throws Exception {
        DataOutput out = DataOutput.success();
        IndvUserInfo indvUserInfo = new IndvUserInfo();
        if (
            input.getParam().get("beltoOrgId") != null &&
                input.getParam().get("beltoOrgId").equals("1")
        ){
            if (
                input.getParam().get("eml") == null ||
                    StringUtils.isEmpty(input.getParam().get("eml").toString())
            ){
                out.setCode(-90001);
                out.setMsg("邮箱为必填项！");
                return out;
            }
        }else if (
                input.getParam().get("beltoOrgId") != null &&
                    !input.getParam().get("beltoOrgId").equals("")&&
                    !input.getParam().get("beltoOrgId").equals("1")
        ){
            if (
                input.getParam().get("dtcId") == null ||
                    input.getParam().get("dtcNo") == null ||
                    StringUtils.isEmpty(input.getParam().get("dtcNo").toString()) ||
                    StringUtils.isEmpty(input.getParam().get("dtcNo").toString())
            ){
                out.setCode(-90001);
                out.setMsg("证件类型和证件号码为必填项！");
                return out;
            }
        }
        if (
            input.getParam() == null ||
            input.getParam().get("indvUserNo") == null ||
            input.getParam().get("indvUserName") == null ||
            input.getParam().get("loginName") == null ||
            input.getParam().get("beltoDeptNo") == null ||
            input.getParam().get("mob") == null ||
            input.getParam().get("eml") == null ||
            StringUtils.isEmpty(input.getParam().get("indvUserNo").toString()) ||
            StringUtils.isEmpty(input.getParam().get("indvUserName").toString()) ||
            StringUtils.isEmpty(input.getParam().get("loginName").toString()) ||
            StringUtils.isEmpty(input.getParam().get("beltoDeptNo").toString()) ||
            StringUtils.isEmpty(input.getParam().get("mob").toString()) ||
            StringUtils.isEmpty(input.getParam().get("eml").toString())
        ){
            out.setCode(-90001);
            out.setMsg("必填字段不存在！");
            return out;
        }
        try {
            indvUserInfo =(IndvUserInfo) ConvertEntityUtil.convertEntityByParam(input.getParam(), indvUserInfo);
            out = indvUserInfoService.indvUserUpdate(indvUserInfo, request);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            out.setCode(-90001);
            out.setMsg("操作失败！" + e.getMessage());
        }
        return out;
    }

    /**
     * 删除个人用户
     * @param input
     * @return
     */
    @CrossOrigin
    @LogParameter(key = "", desc = "删除个人用户")
    @RequestMapping(value = "/deleteIndvUser", method = RequestMethod.POST)
    public DataOutput deleteIndvUser(@RequestBody DataInput input, HttpServletRequest request) throws Exception{
        DataOutput out = DataOutput.success();
        if (
            input.getParam() == null ||
            input.getParam().get("userInfoNo") == null ||
            StringUtils.isEmpty(input.getParam().get("userInfoNo").toString()) ||
            input.getParam().get("perUserNo") == null ||
            StringUtils.isEmpty(input.getParam().get("perUserNo").toString())
        ){
            out.setCode(-90001);
            out.setMsg("缺少必填字段！");
            return out;
        }
        try {
            //indvUserInfoService.deleteIndvUser(input.getParam().get("perUserNo").toString(), request);
            String userInfoNo = input.getParam().get("userInfoNo")!=null && !input.getParam().get("userInfoNo").equals("")?input.getParam().get("userInfoNo").toString():null;
            String perUserNo = input.getParam().get("perUserNo")!=null && !input.getParam().get("perUserNo").equals("")?input.getParam().get("perUserNo").toString():null;
            String openId = input.getParam().get("openId")!=null && !input.getParam().get("openId").equals("")?input.getParam().get("openId").toString():null;

            out = indvUserInfoService.deleteIndvUser(userInfoNo, perUserNo, openId,request);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            out.setCode(-90001);
            out.setMsg("操作失败！" + e.getMessage());
        }
        return out;
    }

    /**
     * 个人用户状态修改
     * @param input
     * @return
     */
    @CrossOrigin
    @LogParameter(key = "", desc = "个人用户状态修改")
    @RequestMapping(value = "/updateUserState", method = RequestMethod.POST)
    public DataOutput updateUserState(@RequestBody DataInput input, HttpServletRequest request) throws Exception{
        DataOutput out = DataOutput.success();
        if (
                input.getParam() == null ||
                        input.getParam().get("userInfoNo") == null ||
                        StringUtils.isEmpty(input.getParam().get("userInfoNo").toString()) ||
                        input.getParam().get("status") == null ||
                        StringUtils.isEmpty(input.getParam().get("status").toString())
        ){
            out.setCode(-90001);
            out.setMsg("缺少必填字段！");
            return out;
        }
        try {
            //indvUserInfoService.deleteIndvUser(input.getParam().get("perUserNo").toString(), request);
            String userInfoNo = input.getParam().get("userInfoNo")!=null && !input.getParam().get("userInfoNo").equals("")?input.getParam().get("userInfoNo").toString():null;
            String status = input.getParam().get("status")!=null && !input.getParam().get("status").equals("")?input.getParam().get("status").toString():null;
            //String openId = input.getParam().get("openId")!=null && !input.getParam().get("openId").equals("")?input.getParam().get("openId").toString():null;

            out = indvUserInfoService.updateUserState(userInfoNo,status);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            out.setCode(-90001);
            out.setMsg("操作失败！" + e.getMessage());
        }
        return out;
    }
    /**
     * 获取当前部门信息
     * @param input
     * @return
     */
    @CrossOrigin
    @LogParameter(key = "", desc = "获取当前部门信息")
    @RequestMapping(value = "/getCurrentDeptInfo", method = RequestMethod.POST)
    public DataOutput getCurrentDeptInfo(@RequestBody DataInput input, HttpServletRequest request) throws Exception{
        DataOutput out = DataOutput.success();
        try {
            SysUserInfo userInfo = UserUtil.getUserInfo(request);
            List<Map<String, String>> list = indvUserInfoService.getCurrentDeptInfo(userInfo);
            out = MybatisParam.getOutput(list);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            out.setCode(-90001);
            out.setMsg("操作失败！");
        }
        return out;
    }

    /**
     * 查询角色用户
     * @param input
     * @param request
     * @return
     */
    @SuppressWarnings("unchecked")
    @ResponseBody
    @RequestMapping("/querySysRoleUserList")
    public DataOutput querySysRoleUserList(@RequestBody DataInput input, HttpServletRequest request)  throws Exception{
        DataOutput out = DataOutput.success();
        if(input.getParam().get("arId") == null ||StringUtils.isEmpty(input.getParam().get("arId").toString())){
            out.setCode(-3000);
            out.setMsg("缺少必填参数，请检查后重试！");
            return  out;
        }
        try {
           List<SysUserInfo> invuserList = indvUserInfoService.querySysRoleUserList(input);

            out = MybatisParam.getOutput(ConvertEntityUtil.convertListData(invuserList));
        } catch (Exception e) {
            e.printStackTrace();
            out.setCode(-91001);
            out.setMsg("查询列表失败!");
        }
        return out;
    }


    /**
     * 查询个人用户所属部门
     * @param request
     * @return
     */
    @CrossOrigin
    @LogParameter(key = "", desc = "查询个人用户所属部门")
    @RequestMapping(value = "/queryIndvUserDept", method = RequestMethod.POST)
    public DataOutput queryIndvUserDept(HttpServletRequest request) throws Exception{
        DataOutput out = DataOutput.success();
        try {
            SysUserInfo userInfo = UserUtil.getUserInfo(request);
            List<Map<String,Object>> mapList = indvUserInfoService.queryIndvUserDept(userInfo);
            out.setData(mapList);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            out.setCode(-90001);
            out.setMsg("操作失败！");
        }
        return out;
    }
}
